using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Microsoft.ApplicationBlocks.Data;

namespace Customs.Components.SXXK
{
    public class DinhMuc
    {
        
        public static bool CheckExist(SqlTransaction transaction, string maHaiQuan, string maDoanhNghiep, string maSP, string maNPL)
        {
            string query = "SELECT COUNT(*) FROM DDINHMUC WHERE (MA_HQ = @MaHaiQuan) AND (MA_DV = @MaDoanhNghiep) AND (Ma_SP = @MaSP) AND (MA_NPL = @MaNPL)";
            SqlParameter[] parameters = new SqlParameter[4];

            parameters[0] = new SqlParameter("@MaHaiQuan", SqlDbType.Char);
            parameters[0].Size = 6;
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = maHaiQuan;

            parameters[1] = new SqlParameter("@MaDoanhNghiep", SqlDbType.VarChar);
            parameters[1].Size = 14;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = maDoanhNghiep;

            parameters[2] = new SqlParameter("@MaNPL", SqlDbType.VarChar);
            parameters[2].Size = 30;
            parameters[2].Direction = ParameterDirection.Input;
            parameters[2].Value = maNPL;
            
            parameters[3] = new SqlParameter("@MaSP", SqlDbType.VarChar);
            parameters[3].Size = 30;
            parameters[3].Direction = ParameterDirection.Input;
            parameters[3].Value = maSP;

            object obj = SqlHelper.ExecuteScalar(transaction, CommandType.Text, query, parameters);
            if (obj != null)
            {
                if (Convert.ToInt32(obj) > 0)
                    return true;
            }
            return false;
        }

        //public static string[] CheckExist(string maHaiQuan, string maDoanhNghiep, string[] danhSachMaSP)
        //{
        //    ArrayList temp = new ArrayList();
        //    string connectionString = ConnectionManager.GetConnectionString("SXXK", maHaiQuan);
        //    using (SqlConnection conn = new SqlConnection(connectionString))
        //    {
        //        conn.Open();
        //        SqlTransaction trans = conn.BeginTransaction();
        //        try
        //        {
        //            foreach (string maNPL in danhSachMaSP)
        //            {
        //                if (CheckExist(trans, maHaiQuan, maDoanhNghiep, maSP))
        //                {
        //                    temp.Add(maNPL);
        //                }
        //            }
        //            trans.Commit();
        //        }
        //        catch (Exception ex)
        //        {
        //            trans.Rollback();
        //            throw new Exception(ex.Message);
        //        }
        //        finally
        //        {
        //            conn.Close();
        //        }
        //    }
        //    return (string[])temp.ToArray(typeof(string));
        //}
        public static DataSet GetDanhSach(string maHaiQuan, string maDoanhNghiep)
        {
            string connectionString = ConnectionManager.GetConnectionString("SXXK", maHaiQuan);
            string query = "SELECT Ma_HQ, Ma_DV, Ma_SP, Ma_NPL, DM_SD, TL_HH, DM_CHUNG, GHI_CHU  FROM DDINHMUC WHERE MA_HQ = @MA_HQ AND MA_DV = @MA_DV";

            SqlParameter[] parameters = new SqlParameter[2];

            parameters[0] = new SqlParameter("@Ma_HQ", SqlDbType.Char);
            parameters[0].Size = 6;
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = maHaiQuan;

            parameters[1] = new SqlParameter("@Ma_DV", SqlDbType.VarChar);
            parameters[1].Size = 14;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = maDoanhNghiep;

            return SqlHelper.ExecuteDataset(connectionString, CommandType.Text, query, parameters);
        }
        public static DataSet GetDanhSachOfSanPham(string maHaiQuan, string maDoanhNghiep,string masp)
        {
            string connectionString = ConnectionManager.GetConnectionString("SXXK", maHaiQuan);
            string query = "SELECT Ma_HQ, Ma_DV, Ma_SP, Ma_NPL, DM_SD, TL_HH, DM_CHUNG, GHI_CHU  FROM DDINHMUC WHERE MA_HQ = @MA_HQ AND MA_DV = @MA_DV and MA_SP=@MA_SP";

            SqlParameter[] parameters = new SqlParameter[3];

            parameters[0] = new SqlParameter("@Ma_HQ", SqlDbType.Char);
            parameters[0].Size = 6;
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = maHaiQuan;

            parameters[1] = new SqlParameter("@Ma_DV", SqlDbType.VarChar);
            parameters[1].Size = 14;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = maDoanhNghiep;

            parameters[2] = new SqlParameter("@Ma_SP", SqlDbType.VarChar);
            parameters[2].Size = 30;
            parameters[2].Direction = ParameterDirection.Input;
            parameters[2].Value = masp;

            return SqlHelper.ExecuteDataset(connectionString, CommandType.Text, query, parameters);
        }
    }
}
